package com.yangmf.auto_message_handle.controller.user;

import com.yangmf.auto_message_handle.pojo.dto.MergeMessageDTO;
import com.yangmf.auto_message_handle.pojo.dto.MesgDTO;
import com.yangmf.auto_message_handle.pojo.entity.Mesg;
import com.yangmf.auto_message_handle.pojo.vo.FileTypeVO;
import com.yangmf.auto_message_handle.result.Result;
import com.yangmf.auto_message_handle.service.DataService;
import com.yangmf.auto_message_handle.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("user/data")
@Tag(name = "数据模块")
@Slf4j
public class DataController {

    @Autowired
    private DataService dataService;

    @Autowired
    private UserService userService;

    @Operation(summary = "获取所有文件分类")
    @GetMapping("/getAllEventType")
    public Result getAllFileType(){

        log.info("获取所有文件分类");

        FileTypeVO filebyType = dataService.getFilebyType();
        return Result.success(filebyType);
    };

    @GetMapping("/getDetailFileByType")
    @Operation(summary = "获取文件分类{}下的文件")
    public Result getDetailFileByType(Long type){
        log.info("获取文件分类{}下的文件",type);
        return Result.success(dataService.getDetailFileByType(type));
    }

    @Operation(summary = "将消息归入事件")
    @PostMapping("/merge")
    public Result mergeEvent(@RequestBody MergeMessageDTO mergeMessageDTO){

        Long eventId = mergeMessageDTO.getEventId();
        List messageIds = mergeMessageDTO.getMessageIds();
        if(messageIds.size()==0||messageIds== null)
          throw new RuntimeException("请选择归入的消息");
        userService.getUserInfo();
        if(userService.getUserInfo().getCharacter()==0)
            return Result.error("无此权限");

        log.info("将消息归入事件{}",eventId);
        dataService.mergeMessageToEvent(eventId,messageIds);
        return Result.success("归入成功");
    }

    @Operation(summary = "检索内容")
    @GetMapping("/search")
    public Result search(
            @RequestParam(required = false) String content,
            @RequestParam(required = false) Integer messageType,
            @RequestParam(required = false) Integer eventType,
            @RequestParam(required = false) Long eventId,
            @RequestParam(required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime createTime

    ) {
       if (createTime == null) {
            createTime= LocalDateTime.now().minusDays(1);
        }
        log.info("检索时间{}", createTime);

        MesgDTO mesgDTO = new MesgDTO();
        mesgDTO.setContent(content);
        mesgDTO.setMessageType(messageType);
        mesgDTO.setEventType(eventType);
        mesgDTO.setEventId(eventId);
        mesgDTO.setCreateTime(createTime);

        log.info("检索内容{}", mesgDTO);
        return Result.success(dataService.search(mesgDTO));
    }

    @Operation(summary = "批量删除message")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Long> messageIds){
        log.info("批量删除message{}",messageIds);
        dataService.deleteMessage(messageIds);
        return Result.success("删除成功");
    }

}
